package com.webex.ta.hydra.gui.util;

import com.webex.ta.hydra.gui.GuiPackage;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.io.File;
import java.util.prefs.Preferences;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-9-6
 * Time: 8:41:40
 */
public final class FileDialoger {
	/**
	 * The last directory visited by the user while choosing Files.
	 */
	private static String lastJFCDirectory = null;

	private static JFileChooser jfc = new JFileChooser();

    private static Preferences prefs = Preferences.userNodeForPackage(FileDialoger.class);

    private static final String RECENT_ACCESS_KEY = "recent_access";

    /**
	 * Prevent instantiation of utility class.
	 */
	private FileDialoger() {
	}

	/**
	 * Prompts the user to choose a file from their filesystems for our own
	 * devious uses. This method maintains the last directory the user visited
	 * before dismissing the dialog. This does NOT imply they actually chose a
	 * file from that directory, only that they closed the dialog there. It is
	 * the caller's responsibility to check to see if the selected file is
	 * non-null.
	 *
	 * @return the JFileChooser that interacted with the user, after they are
	 *         finished using it (accept or otherwise).
	 */
	public static JFileChooser promptToOpenFile(String[] exts) {
//		JFileChooser jfc = null;

        lastJFCDirectory = getRecentAccessPath();
        if (lastJFCDirectory == null) {
			String start = System.getProperty("user.dir", ""); //$NON-NLS-1$//$NON-NLS-2$

			if (start.length() > 0) {
				jfc.setCurrentDirectory(new File(start));
			}
		} else {
            jfc.setCurrentDirectory(new File(lastJFCDirectory));
        }
		clearFileFilters();
        if(exts != null && exts.length > 0) {
    		jfc.addChoosableFileFilter(new HydraFileFilter(exts));
        }
		int retVal = jfc.showOpenDialog(GuiPackage.getInstance().getIdeFrame());
		lastJFCDirectory = jfc.getCurrentDirectory().getAbsolutePath();


        if (retVal == JFileChooser.APPROVE_OPTION) {
            setRecentAccessPath(lastJFCDirectory);
            return jfc;
		} else {
			return null;
		}
	}

	private static void clearFileFilters() {
		FileFilter[] filters = jfc.getChoosableFileFilters();
		for (int x = 0; x < filters.length; x++) {
			jfc.removeChoosableFileFilter(filters[x]);
		}
	}

	public static JFileChooser promptToOpenFile() {
		return promptToOpenFile(new String[0]);
	}

	/**
	 * Prompts the user to choose a file from their filesystems for our own
	 * devious uses. This method maintains the last directory the user visited
	 * before dismissing the dialog. This does NOT imply they actually chose a
	 * file from that directory, only that they closed the dialog there. It is
	 * the caller's responsibility to check to see if the selected file is
	 * non-null.
	 *
	 * @return the JFileChooser that interacted with the user, after they are
	 *         finished using it (accept or otherwise).
	 * @see #promptToOpenFile()
	 */
	public static JFileChooser promptToSaveFile(String filename) {
		return promptToSaveFile(filename, null);
	}

	/**
	 * Get a JFileChooser with a new FileFilter.
	 *
	 * @param filename file name
	 * @param extensions list of extensions
	 * @return the FileChooser
	 */
	public static JFileChooser promptToSaveFile(String filename, String[] extensions) {
        lastJFCDirectory = getRecentAccessPath();
        if (lastJFCDirectory == null) {
			String start = System.getProperty("user.dir", "");
			if (start.length() > 0) {
				jfc = new JFileChooser(new File(start));
			}
			lastJFCDirectory = jfc.getCurrentDirectory().getAbsolutePath();
            setRecentAccessPath(lastJFCDirectory);
        }
		String ext = ".jmx";//$NON-NLS-1$
		if (filename != null) {
			jfc.setSelectedFile(new File(lastJFCDirectory, filename));
			int i = -1;
			if ((i = filename.lastIndexOf(".")) > -1) {//$NON-NLS-1$
				ext = filename.substring(i);
			}
		}
		clearFileFilters();
		if (extensions != null) {
			jfc.addChoosableFileFilter(new HydraFileFilter(extensions));
		} else {
			jfc.addChoosableFileFilter(new HydraFileFilter(new String[] { ext }));
		}

		int retVal = jfc.showSaveDialog(GuiPackage.getInstance().getIdeFrame());
		lastJFCDirectory = jfc.getCurrentDirectory().getAbsolutePath();

        if (retVal == JFileChooser.APPROVE_OPTION) {
            setRecentAccessPath(lastJFCDirectory);
            return jfc;
		} else {
			return null;
		}
	}

    private static String getRecentAccessPath()  {
        return prefs.get(RECENT_ACCESS_KEY, null);
    }

    private static void setRecentAccessPath(String path)  {
        prefs.put(RECENT_ACCESS_KEY, path);
    }
}
